package main

import (
	"github.com/judwhite/go-svc/svc"
	"path/filepath"
	log "github.com/sirupsen/logrus"
	"twokick/pkg/logrus_config"
	"twokick/conf"
	"github.com/BurntSushi/toml"
	"syscall"
	"runtime"
	"os"
	"twokick/internal/http/server"
	"twokick/internal/http/service"
)
var (
	c conf.HttpConf
	scv *service.Service
)
func main(){
	if _, e := toml.DecodeFile("./config.toml", &c); e != nil {
		panic(e)
	}
	prg := &program{}
	if err := svc.Run(prg, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT); err != nil {
		panic(err)
	}
}
type program struct {}
func (*program) Init(env svc.Environment) error {
	logrus_config.ConfigLocalFileSystemLogger(c.Log.Dir,c.Log.FileName,c.Log.Level)
	if env.IsWindowsService() {
		dir := filepath.Dir(os.Args[0])
		return os.Chdir(dir)
	}
	return nil
}
func (*program) Start() error {
	log.Info("twokick start")

	runtime.GOMAXPROCS(runtime.NumCPU())
	scv = service.New(&c)
	engine := server.New(scv)
	go engine.Run(c.BindAddr)

	return nil
}
func (*program) Stop() error {
	log.Info("twokick stop")
	scv.Close()
	return nil
}

